Private Sub PagingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Evaluate number of pages.
GetPageNumber()
' Bind the Titles table.
DataGrid1.DataSource = dt
' Show the first page of results
btnFirst.PerformClick()
End Sub
' Evaluate number of pages in the result.
Sub GetPageNumber()
Dim closeOnExit As Boolean
' Open the connection if necessary.
If cn.State = ConnectionState.Closed Then
cn.Open()
closeOnExit = True
End If
' Evaluate number of records.
cmd.CommandText = "SELECT COUNT(*) FROM Titles"
recCount = CInt(cmd.ExecuteScalar())
' Close the connection if it was closed.
If closeOnExit Then cn.Close()
' Evaluate number of pages.
pageCount = (recCount + pageSize - 1) \ pageSize
lblRecords.Text = " of " & pageCount.ToString
End Sub
' Display the N-th page of result.
Sub DisplayPage(ByVal n As Integer, ByVal sql As String)
' Perform the query and display results.
cn.Open()
Dim da As New OleDbDataAdapter(sql, cn)
dt.Clear()
da.Fill(dt)
' Uncomment next statement to update page count each time
' the user displays a new page.
' GetPageNumber
cn.Close()
' Remember current page number and display it.
currPage = n
lblCurrPage.Text = n.ToString
' Enable/disable buttons.
btnFirst.Enabled = (n > 1)
btnPrevious.Enabled = (n > 1)
btnNext.Enabled = (n < pageCount)
btnLast.Enabled = (n < pageCount)
End Sub
' Manage the four navigational buttons.
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
sql = String.Format("SELECT TOP {0} * FROM Titles ORDER BY ISBN", pageSize)
DisplayPage(1, sql)
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles WHERE ISBN < '{1}' ORDER BY ISBN DESC) ORDER BY ISBN", pageSize, dt.Rows(0)("ISBN"))
DisplayPage(currPage - 1, sql)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
sql = String.Format("SELECT TOP {0} * FROM Titles WHERE ISBN > '{1}' ORDER BY ISBN", pageSize, dt.Rows(dt.Rows.Count - 1)("ISBN"))
DisplayPage(currPage + 1, sql)
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
' Evaluate number or records on last page.
Dim num As Integer = recCount - pageSize * (pageCount - 1)
sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles ORDER BY ISBN DESC) ORDER BY ISBN", num)
DisplayPage(pageCount, sql)
End Sub
' Goto N-th page.
Private Sub btnGoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoto.Click
Try
Dim pageNum As Integer = CInt(txtPageNum.Text)
sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles WHERE ISBN IN (SELECT TOP {1} ISBN FROM Titles ORDER BY ISBN) ORDER BY ISBN DESC) ORDER BY ISBN", pageSize, pageSize * pageNum)
DisplayPage(pageNum, sql)
Catch ex As Exception
Debug.WriteLine(ex.Message)
MessageBox.Show("Page # must be in the range [1," & pageCount.ToString & "]")
End Try
End Sub
' Enable the Goto button only if something has been typed inside the TextBox.
Private Sub txtPageNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPageNum.TextChanged